<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/28 0028
 * Time: 上午 9:11
 */

namespace app\sports_admin\controller;

use app\sports_admin\service\SportsGame as SportsGameService;
use app\sports_admin\vaildate\SportsGame as SportsGameVaildate;
use OSS\Core\OssException;
use OSS\OssClient;
use think\Config;

class SportsGame extends Common
{
    //运动游戏首页
    public function index()
    {
        return view('index');
    }
    //游戏列表
    public function sportsGameList()
    {
        return SportsGameService::sportsGameList();
    }
    //添加游戏页面
    public function add()
    {
        return view('add');
    }
    //添加运动游戏
    public function insert()
    {
        (new SportsGameVaildate())->insert();
        return SportsGameService::insert();
    }
    //删除运动游戏
    public function delete()
    {
        (new SportsGameVaildate())->go_check();
        return SportsGameService::delete();
    }
    /**
     * 图片上传
     */
    public function upload_image()
    {
        //获取表单上传文件
        $file = request()->file('file');
        //获取文件存储路径
        $path = '/public/uploads/sports_game/img/';
        if($file){
            $info = $file->move(ROOT_PATH."$path");
            $route= $info->getSaveName();
            if($info){
                //上传成功后返回路径
                return json([
                    'code'=>200,
                    'msg'=>'/uploads/sports_game/img/'.$route,
                ]);
            }else{
                // 上传失败获取错误信息
                return json(['error'=>$file->getError()]);
            }
        }
    }
    /**
     * 上传视频
     */
    public function upload_video()
    {
        $scr = $_FILES['file']['tmp_name'];
        $ext = substr($_FILES['file']['name'],strrpos($_FILES['file']['name'],'.')+1); // 上传文件后缀
        $dst = md5(time()).'-'.$scr.'.'.$ext;     //上传文件名称
        // $this->load->library('AliUpload');
        $url = $this->aLiYun($dst,$scr);
        return json(['video'=>$url,'code'=>'0']);
    }
    /**
     * 阿里云上传视频
     * @param $dst 文件名
     * @param $src 临时文件名
     */
    public function aLiYun($dst,$src)
    {
        $aLiYunOss =  Config::get("aLiYunOss");
        $auth = new OssClient($aLiYunOss['key_id'],$aLiYunOss['key_secret'],$aLiYunOss['endPoint']);
        try {
            //上传图片
            $result  = $auth->uploadFile($aLiYunOss['bucket'],$dst,$src);
            return $result['info']['url'];
        } catch (OssException $e) {
            return $e->getMessage();
        }
    }
}